package com.jichangxiu.framework.entity.bo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jichangxiu.common.entity.rule.RoleRule;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;

import java.io.Serializable;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class SecurityRole implements GrantedAuthority, Serializable {

    private static final long serialVersionUID = 1L;

    public static final String SECURITY_ROLE_PREFIX = "ROLE_";

    private RoleRule roleRule;

    public String getRoleId() {
        return roleRule.getRoleId();
    }

    public String getRoleCode() {
        return roleRule.getRoleCode();
    }

    public String getRoleName() {
        return roleRule.getRoleName();
    }

    @Override
    @JsonIgnore
    public String getAuthority() {
        String roleCode = getRoleCode();
        return roleCode.startsWith(SECURITY_ROLE_PREFIX) ? roleCode : SECURITY_ROLE_PREFIX + roleCode;
    }

}
